module z {
	/**
	 * 树
	 */
	export class tree extends eui.Component implements eui.UIComponent {
		private tree: eui.Image;
		private autotree: eui.Image;
		private manone: boolean = false
		private arr: Array<any> = new Array<any>()
		private chixu: number;
		private item: number = 1;
		private ys = 466
		private ns = 0
		private s: boolean = false
		private pao: boolean = false
		private autos: boolean = false
		private maxspeed: number = 26
		private di: eui.Image;
		private cao: eui.Image;
		private lanshan: eui.Image;
		private lvshan: eui.Image;
		private qipao: eui.Image;
		private qipaonum: eui.Label;
		private big: egret.Sprite;
		private yuncai1: eui.Image;
		private yuncai2: eui.Image;
		private yuncai3: eui.Image;
		private yuncai4: eui.Image;
		private num: number = 0;
		public isgoing: boolean = false;

		private a: number = 0;//加速度
		private z: number = 0.95;//阻力
		public constructor() {
			super();
		}
		protected childrenCreated(): void {
			super.childrenCreated();
			this.inits();
			this.adds()
			eui.Watcher.watch(this, ["isgoing"], this.watcherHander, this);

		}
		public watcherHander(v: any) {
			console.log(v)
		}
		/**
		 * 添加刻度
		 */
		private adds() {
			for (let i = 0; i < 24; i++) {
				let s: eui.Label = new eui.Label();
				s.name = i + ""
				s.x = 1700
				s.fontFamily = "fz"
				s.bold = true
				s.y = this.ys;
				this.ys -= 90;
				s.text = "——" + this.ns
				this.ns += 1;
				this.addChild(s)
				this.arr.push(s)
			}

		}
		/**
		 * 获取最后的刻度位置
		 */
		private getzuixiao() {
			let s = 0;
			this.arr.forEach((v, i) => {
				if (v != this.tree && v != this.autotree) {
					if (v.y < s) {
						s = v.y
					}
				}
			})
			return s;
		}

		/**
		 * 正常速度（全场只开一次)
		 */
		public auto() {
			this.qipao.visible = true
			this.qipaonum.visible = true
			if (this.autos == false) {
				this.autos = true
				if (this.manone == false) {
					this.chixu = setInterval(() => {

						if (this.yuncai1.y >= 1080) {
							this.yuncai1.y = -500
						}
						if (this.yuncai2.y >= 1080) {
							this.yuncai2.y = -1400
						}

						if (this.yuncai3.y >= 1080) {
							this.yuncai3.y = -2500
						}

						if (this.yuncai4.y >= 1080) {
							this.yuncai4.y = -3600
						}

						// if (this.num < 0) {
						// 	this.item = 20
						// } else if (this.num == 1) {
						// 	this.item = 10
						// } else if (this.num == 2) {
						// 	this.item = 10
						// } else if (this.num == 3) {
						// 	this.item = 4
						// } else if (this.num == 4) {
						// 	this.item = 1
						// }

						this.item += this.a
						if (this.item > 1) {
							this.item = this.item * this.z
						}
						if (this.item > 50) {
							this.item = 50
						}

						this.yuncai1.y += this.item * 0.1
						this.yuncai2.y += this.item * 0.1
						this.yuncai3.y += this.item * 0.1
						this.yuncai4.y += this.item * 0.1
						this.lvshan.y += this.item * 0.33
						this.lanshan.y += this.item * 0.1
						this.di.y += this.item
						this.cao.y += this.item
						this.arr.forEach((v) => {
							if (v.y + this.item >= 1080) {
								let sss = v.y - 1080;
								if (v == this.tree) { v.y = -1080 + this.autotree.y }
								if (v == this.autotree) { v.y = -1080 + this.tree.y }
								if (v != this.tree && v != this.autotree) {
									v.y = this.getzuixiao() - 90 - this.item
									v.text = "——" + this.ns;
									this.ns += 1
								}
							}
							v.y += this.item

						})
						this.arr.forEach((v) => {
							if (v != this.tree && v != this.autotree) {
								if (v.y > 200 && v.y < 600) {
									if (v.y > 450) {
										let sss = String(v.text)
										sss = sss.replace("——", "")
										sss = sss.replace("——", "")
										this.qipaonum.text = sss
										return
									}
								}


							}



						})










					}, 20)
				}
				this.manone = true;
			}
		}


		/**
		  *眩晕
		  */
		public pause() {
			this.item = 0
			this.isgoing = false;
			this.a = 0
		}
		/**
		*恢复
		*/
		public ok() {
			this.item = 1
			this.a = 0
		}
		/**
		  *结束
		  */
		public stop() {
			this.item = 0
			this.a = 0
			this.qipao.visible = false
			this.qipaonum.visible = false
			return Number(this.qipaonum.text)

		}
		/**
		 * 回答错误调用
		 */
		public err() {
			this.item = 0
			this.isgoing = false;
			this.a = 0

		}
		/**
		 * 回答正确调用
		 */
		public fastgo() {

			this.pao = true;
			if (this.s == false) {
				this.ok()
				this.go();
			} else {
				if (this.a != 0) {
					this.a += 1;

				}



			}


		}
		/**
		 * 加速基类
		 */
		private go() {
			this.s = true;
			this.a = 1;
			this.isgoing = true;

			// this.item = 10
			// this.num += 1;
			let s = setInterval(() => {

				this.a = 0;
				this.s = false;
				window.clearInterval(s)




			}, 2000)

		}







		public init() {
			this.qipao.visible = true
			this.qipaonum.visible = true
			this.yuncai1.x = 200
			this.yuncai1.y = -200

			this.yuncai2.x = 900
			this.yuncai2.y = -900

			this.yuncai3.x = 700
			this.yuncai3.y = -1590

			this.yuncai4.x = 590
			this.yuncai4.y = -2600
			this.lanshan.x = 0
			this.lanshan.y = 1080 - this.lanshan.height
			this.lvshan.x = 0
			this.lvshan.y = 1080 - this.lvshan.height
			this.cao.x = 0
			this.cao.y = 1080 - this.cao.height
			this.tree.x = 1690;
			this.tree.y = -500;
			this.autotree.x = 1690;
			this.autotree.y = -1580;
			this.di.x = 1439
			this.di.y = 1080 - this.di.height

			this.ys = 466
			this.ns = 0
			this.arr.forEach((v, i) => {
				if (v != this.tree && v != this.autotree) {
					v.y = this.ys;
					this.ys -= 90;
					v.text = "——" + this.ns
					this.ns += 1;
				}

			})

		}
		/**
		 * 初始化
		 */
		private inits() {
			this.jianbianbg()

			///////////////////////////////////蓝山
			this.lanshan = new eui.Image()
			this.lanshan.source = "lan_png";
			this.addChild(this.lanshan)
			this.lanshan.x = 0
			this.lanshan.y = 1080 - this.lanshan.height
			///////////////////////////////////绿山
			this.lvshan = new eui.Image()
			this.lvshan.source = "lv_png";
			this.addChild(this.lvshan)
			this.lvshan.x = 0
			this.lvshan.y = 1080 - this.lvshan.height
			///////////////////////////////////////////////云
			this.yuncai()
			///////////////////////////////////草地
			this.cao = new eui.Image()
			this.cao.source = "cao_png";
			this.addChild(this.cao)
			this.cao.x = 0
			this.cao.y = 1080 - this.cao.height
			//////////////////////////////////树
			this.tree = new eui.Image()
			this.tree.source = "mutouduan_png";
			this.addChild(this.tree)
			this.tree.x = 1690;
			this.tree.y = -500;
			this.autotree = new eui.Image()
			this.autotree.source = "mutouduan_png";
			this.addChild(this.autotree)
			this.autotree.x = 1690;
			this.autotree.y = -1580;
			this.arr.push(this.tree)
			this.arr.push(this.autotree)

			//////////////////////////////////树根
			this.di = new eui.Image()
			this.di.source = "shugen_png";
			this.addChild(this.di)
			this.di.x = 1439
			this.di.y = 1080 - this.di.height

			this.qipaoadd();


		}
		/**
		 * 添加气泡
		 */
		private qipaoadd() {
			this.qipao = new eui.Image()
			this.qipao.source = "qipao_png";
			this.addChild(this.qipao)
			this.qipao.x = 1425
			this.qipao.y = 430
			this.qipao.visible = false

			this.qipaonum = new eui.Label()
			this.qipaonum.visible = false
			this.addChild(this.qipaonum)
			this.qipaonum.fontFamily = "fz"
			this.qipaonum.bold = true
			this.qipaonum.x = 1550
			this.qipaonum.y = 454
			this.qipaonum.text = 0 + ""
			this.qipaonum.textColor = 0x0d79bf
			this.qipaonum.size = 45
		}

		private jianbianbg() {
			let rect: eui.Rect = new eui.Rect();
			rect.fillColor = 0xFFFFFF
			rect.x = 0
			rect.y = 0
			rect.height = 1080
			rect.width = 1920
			this.addChild(rect)

			this.big = new egret.Sprite();
			let m: egret.Matrix = this.big.matrix;
			this.big.graphics.beginGradientFill(egret.GradientType.LINEAR, [0x5AB5FC, 0x5AB5FC], [1, 1], [0, 255], m);
			this.big.graphics.drawRect(0, 0, 1920, 1080);
			this.big.graphics.endFill()
			this.addChild(this.big)
		}


		private yuncai() {
			this.yuncai1 = new eui.Image()
			this.yuncai1.source = "yuncai1_png";
			this.addChild(this.yuncai1)
			this.yuncai1.x = 200
			this.yuncai1.y = -200

			this.yuncai2 = new eui.Image()
			this.yuncai2.source = "yuncai2_png";
			this.addChild(this.yuncai2)
			this.yuncai2.x = 900
			this.yuncai2.y = -900

			this.yuncai3 = new eui.Image()
			this.yuncai3.source = "yuncai3_png";
			this.addChild(this.yuncai3)
			this.yuncai3.x = 700
			this.yuncai3.y = -1590

			this.yuncai4 = new eui.Image()
			this.yuncai4.source = "yuncai4_png";
			this.addChild(this.yuncai4)
			this.yuncai4.x = 590
			this.yuncai4.y = -2600
		}
	}
}